#include "loginwindow.h"
#include "ui_loginwindow.h"
#include "registerwindow.h"
#include "mainwindow.h"

// 构造函数
LoginWindow::LoginWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::LoginWindow)
    , registerWindow(nullptr)
    , mainWindow(nullptr)
{
    ui->setupUi(this);
    
    // 连接信号槽
    connect(ui->loginButton, &QPushButton::clicked, this, &LoginWindow::onLoginClicked);
    connect(ui->registerButton, &QPushButton::clicked, this, &LoginWindow::onRegisterClicked);
    
    // 设置窗口图标和标题
    setWindowTitle("仓库管理系统 - 登录");
    
    // 设置输入框回车事件
    connect(ui->passwordLineEdit, &QLineEdit::returnPressed, this, &LoginWindow::onLoginClicked);
}

// 析构函数
LoginWindow::~LoginWindow()
{
    delete ui;
    if (registerWindow) {
        delete registerWindow;
    }
    if (mainWindow) {
        delete mainWindow;
    }
}

// 登录按钮点击事件
void LoginWindow::onLoginClicked()
{
    if (!validateInput()) {
        return;
    }
    
    QString username = ui->usernameLineEdit->text().trimmed();
    QString password = ui->passwordLineEdit->text();
    
    UserInfo userInfo;
    if (DatabaseManager::getInstance().validateUser(username, password, userInfo)) {
        // 登录成功，创建并显示主窗口
        if (mainWindow) {
            delete mainWindow;
        }
        mainWindow = new MainWindow();
        mainWindow->setCurrentUser(userInfo);
        mainWindow->show();
        
        // 隐藏登录窗口
        this->hide();
        
        // 连接主窗口的退出登录信号
        connect(mainWindow, &MainWindow::logoutRequested, this, [this]() {
            // 先隐藏主窗口
            mainWindow->hide();
            // 删除主窗口实例以释放内存
            mainWindow->deleteLater();
            mainWindow = nullptr;
            // 显示登录窗口
            this->show();
            // 清空输入框
            ui->usernameLineEdit->clear();
            ui->passwordLineEdit->clear();
            ui->usernameLineEdit->setFocus();
        });
    } else {
        showError("用户名或密码错误！");
    }
}

// 注册按钮点击事件
void LoginWindow::onRegisterClicked()
{
    if (!registerWindow) {
        registerWindow = new RegisterWindow(this);
    }
    
    // 连接注册成功信号
    connect(registerWindow, &RegisterWindow::registrationSuccessful, this, [this]() {
        showSuccess("注册成功！请登录。");
        ui->usernameLineEdit->setFocus();
    });
    
    registerWindow->show();
}

// 验证输入
bool LoginWindow::validateInput()
{
    QString username = ui->usernameLineEdit->text().trimmed();
    QString password = ui->passwordLineEdit->text();
    
    if (username.isEmpty()) {
        showError("请输入用户名！");
        ui->usernameLineEdit->setFocus();
        return false;
    }
    
    if (password.isEmpty()) {
        showError("请输入密码！");
        ui->passwordLineEdit->setFocus();
        return false;
    }
    
    return true;
}

// 显示错误信息
void LoginWindow::showError(const QString& message)
{
    QMessageBox::warning(this, "登录错误", message);
}

// 显示成功信息
void LoginWindow::showSuccess(const QString& message)
{
    QMessageBox::information(this, "提示", message);
}